import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { getUserDto } from 'src/user/dto/get-user.dto';
import { UserService } from 'src/user/user.service';

@Injectable()
export class AuthService {
    constructor(
        private userService: UserService,
        private jwt: JwtService
    ) {}
    async signin(username: string, password: string) {
        // const res = await this.userService.findAll({ username } as getUserDto)
        const user = await this.userService.find(username);
        if (user.password === password) {
            const payload = { sub: user.id, username: user.username };
            const tokens = await this.jwt.signAsync(payload, { expiresIn: '1d', secret: 'SECRET' })
            // 生产token
            console.log(tokens)
            return tokens
        }
        throw new UnauthorizedException();
    }

    async signup(username: string, password: string) {
        const res = await this.userService.create({ username, password })
        console.log(res)
        return `这是用户名：${username} 和 密码：${password}, 还有测试${res.data}`
    }
}
